package com.cloudspace.yunloan.ui;


import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.cloudspace.yunloan.R;


/**
 * @author wangning
 */
public class MyScrollView extends ScrollView {
    /**
     * 定义接口
     */
    public static interface OnScrollListener {
        void onScroll(int left, int top, int oldLeft, int oldTop);

        void onScrollDirection(float diff, boolean up);
    }

    public static final int contentViewId = 103234234;

    private Context myContext;

    // 滑动距离及坐标   支持viewpager 左右滑动相应 事件不会被抢占
    private boolean isSupportViewPager = false;
    private float xDistance, yDistance, xLast, yLast;

    private LinearLayout rootView;
    private LinearLayout headView;
    private LinearLayout contentView;

    public MyScrollView(Context context) {
        super(context);
        init(context, null);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (null != onScrollListener) {
            onScrollListener.onScroll(l, t, oldl, oldt);
        }
    }

    private void init(Context context, AttributeSet attrs) {
        this.myContext = context;
        setFadingEdgeLength(0);
        setVerticalScrollBarEnabled(false);
        addView(getMyRootView());

        if (null != attrs) {
            isSupportViewPager = attrs.getAttributeBooleanValue(R.styleable.PullToRefresh_ptrSupportViewPager, false);
        }
    }

    public LinearLayout getMyRootView() {
        if (rootView == null) {
            rootView = new LinearLayout(myContext);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            rootView.setLayoutParams(lp);
            rootView.setOrientation(LinearLayout.VERTICAL);
        }
        return rootView;
    }

    public LinearLayout getMyHeadView(int margin) {
        if (headView == null) {
            headView = new LinearLayout(myContext);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            lp.setMargins(margin, 0, margin, 0);
            headView.setLayoutParams(lp);
            headView.setOrientation(LinearLayout.VERTICAL);
            getMyRootView().addView(headView);
        }
        return headView;
    }

    public LinearLayout getMyContentView(int margin, int orientaion) {
        if (contentView == null) {
            contentView = new LinearLayout(myContext);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            lp.setMargins(margin, 0, margin, 0);
            contentView.setLayoutParams(lp);
            contentView.setOrientation(orientaion);
            getMyRootView().addView(contentView);
        }
        return contentView;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                float yDiff = curY - yLast;
                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(yDiff);
                xLast = curX;
                yLast = curY;

                if (null != onScrollListener) {
                    onScrollListener.onScrollDirection(yDiff, yDiff <= 0);
                }
                if (isSupportViewPager && xDistance > yDistance) {
                    return false;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }

    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    private OnScrollListener onScrollListener;

    public OnScrollListener getOnScrollListener() {
        return onScrollListener;
    }


}